package com.ssm.pre.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.pre.dao.WechatOrderDao;
import com.ssm.pre.service.WechatOrderService;
import com.ssm.pre.util.PageCount;
import com.ssm.util.Constants;
import com.ssm.util.object.InputObject;
import com.ssm.util.object.OutputObject;
import com.ssm.wechat.service.GetWechatJSSDKParameter;
import com.ssm.wechat.service.UnifiedorderService;

@Service
public class WechatOrderServiceImpl implements WechatOrderService {

	@Autowired
	private WechatOrderDao wechatOrderDao;
	
	/**
	 * 物业费缴费获取支付标识
	 */
	@Override
	public void getOrderParameter(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getParams(); 
		
		String key = Constants.KAY;
		String out_trade_no = params.get("out_trade_no").toString();// 商户订单号
		String total_fee = params.get("orderFee").toString();// 标记金额
		String openid = params.get("openid").toString();
		
		//获取预支付标识
		Map<String, Object> message = UnifiedorderService.getPrepayid(key,out_trade_no, total_fee, openid, inputObject, outputObject);// 获取预支付标示
		Map<String, Object> map = new HashMap<>();
		map.put("noncestr", message.get("nonceStr"));
		map.put("timestamp", message.get("timeStamp"));
		map.put("signType", "MD5");
		map.put("appId", Constants.APP_ID);
		map.put("paypackage", message.get("package"));
		map.put("paySign", message.get("paySign"));
		map.put("signature", GetWechatJSSDKParameter.getWechatJsParameter(map).get("signature"));
		
		//返回订单编号 和商家id 
		map.put("orderNumber", params.get("orderNumber").toString());
		map.put("orderId", params.get("orderId"));
		outputObject.setBean(map);
	}

	/**
	 * 电费缴费获取支付标识
	 */
	@Override
	public void getOrderParameter2(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getParams(); 
		
		String key = Constants.KAY;
		String out_trade_no = params.get("out_trade_no").toString();// 商户订单号
		String total_fee = params.get("orderFee").toString();// 标记金额
		String openid = params.get("openid").toString();
		
		//获取预支付标识
		Map<String, Object> message = UnifiedorderService.getPrepayid(key,out_trade_no, total_fee, openid, inputObject, outputObject);// 获取预支付标示
		Map<String, Object> map = new HashMap<>();
		map.put("noncestr", message.get("nonceStr"));
		map.put("timestamp", message.get("timeStamp"));
		map.put("signType", "MD5");
		map.put("appId", Constants.APP_ID);
		map.put("paypackage", message.get("package"));
		map.put("paySign", message.get("paySign"));
		map.put("signature", GetWechatJSSDKParameter.getWechatJsParameter2(map).get("signature"));
		
		//返回订单编号 和商家id 
		map.put("orderNumber", params.get("orderNumber").toString());
		map.put("orderId", params.get("orderId"));
		outputObject.setBean(map);
	}

	/**
	 * 查找微信物业支付订单
	 */
	@Override
	public void queryPropertyOrderByList(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> beans = wechatOrderDao.queryPropertyOrderByList(map);
		int page = Integer.parseInt(map.get("offset").toString())/Integer.parseInt(map.get("limit").toString());
		page++;
		List<Map<String, Object>> params = PageCount.page(beans, page, Integer.parseInt(map.get("limit").toString()));
		outputObject.setBeans(params);
		outputObject.settotal(beans.size());
		
	}

	/**
	 * 查找微信电费支付订单
	 */
	@Override
	public void queryElectricOrderByList(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> beans = wechatOrderDao.queryElectricOrderByList(map);
		int page = Integer.parseInt(map.get("offset").toString())/Integer.parseInt(map.get("limit").toString());
		page++;
		List<Map<String, Object>> params = PageCount.page(beans, page, Integer.parseInt(map.get("limit").toString()));
		outputObject.setBeans(params);
		outputObject.settotal(beans.size());
		
	}

}
